﻿//Copyright Notice:  ©2009 Microsoft Corporation.  All rights reserved.
using System;
using System.Collections.Generic;

[assembly: CLSCompliant(true)]

namespace Microsoft.InformationSecurity.CISF.Security.AuthZServices
{
    /// <summary>
    /// Contains all the global public variables for CISFLDAP module.
    /// </summary>
    public static class AuthZGlobals
    {
        #region Public Variables

        public static string UsersAccount = "UsersAccount";
        public static string DistributionList = "DistributionList";

        #region NamePart
        public enum NamePart
        {
            Domain = 0,
            sAMAccountName = 1
        }

        #endregion

        #region PropertyType
        public enum PropertyType
        {
            None = 0,
            cn = 1,
            DisplayName = 2,
            DistinguishedName = 3,
            PersonnelNumber = 4,
            sAMAccountName = 5,
            sAMAccountType = 6,
            MemberOfDL = 7,
            MemberOfSG = 8,
            MemberDL = 9,
            MemberSG = 10,
            MemberUser = 11,
            TopSG = 12,
            Role = 13,
            DescendantSG = 14,
            DescendantDL = 15,
            RelatedSG = 16,
            RelatedDL = 17,
            RelatedUser = 18,
            PersonData = 19
        }

        #endregion

        #region sAMAccountType

        public enum sAMAccountType
        {
            None = 0x00000000,
            SecurityGroup = 0x10000000,
            DistributionList = 0x10000001,
            DomainLocalGroup = 0x20000000,
            User = 0x30000000,
            ServiceAccount = 0x30000000,
            Workstation = 0x30000001
        }

        #endregion

        #region RDNPrefix

        public class RDNPrefix : SpecializedEnum<RDNPrefix, string>
        {
            public static readonly string CommonName = "CN=";
            public static readonly string DomainComponent = "DC=";
            public static readonly string OrganizationUnitName = "OU=";
            public static readonly string Organization = "O=";
            public static readonly string UserId = "UID=";
        }

        #endregion

        #endregion

        #region ParseName
        /// <summary>
        /// Parse the name and isolate the sAMAccountName from the domain.
        /// </summary>
        /// <param name="name">The name to parse. </param>
        /// <returns>Two part string array containing Domain and sAMAccountName.</returns>
        static public string[] ParseName(string name)
        {
            string[] nameParts = new string[2];

            nameParts[0] = "";

            char[] separator = new char[] { '\\' };

            string[] authUser = name.Split(separator, StringSplitOptions.None);

            //If no domain separator is found, assume it's just an alias.
            if (authUser.Length == 1)
                nameParts[1] = authUser[0];
            else if (authUser.Length > 1)
            {
                nameParts[0] = authUser[0];
                nameParts[1] = authUser[1];
            }
            else
                throw new ArgumentException("Invalid name format.");

            return nameParts;
        }

        #endregion

    }
}
